//引入插件：微信同声传译
const plugin = requirePlugin("WechatSI");
// 获取**全局唯一**的语音识别管理器**recordRecoManager**
const manager = plugin.getRecordRecognitionManager();
// 打分工具类
const scoreutil = require('../../utils/scoreutil.js');
const log = require('../../utils/log.js')
let isRecording = false; // false 没有录音 true 正在录音

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      observer: function(newVal, oldVal) {
        console.log("newVal="+newVal+",oldVal="+oldVal,",isRecording=",isRecording)
        if (newVal && !oldVal && !isRecording) {
          manager.start({
            lang: "en_US",
            format: "mp3"
          })
          isRecording =true;
          console.log("managerstart")
        } 
        
        if(!newVal && oldVal && isRecording){
          manager.stop();
          isRecording=false;
          console.log("managerstop");
        }
      }
    },
    position: {
      type: Object,
    },
    originalStr: {
      type: String
    },
    mask: {
      type: Object
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },
  created: function() {

  },
  ready: function() {
    var that = this;
    manager.onRecognize = function(res) {
      console.log("onRecognize")
    };
    manager.onStop = function(res) {
      console.log("record---onStop")
      isRecording=false;
      if (that.data.show) {
        that.setData({
          show: false
        });
      }
      let score;
      console.log("onstop--识别的结果为："+res.result);
      if (res.result == '') {
        // score = -1
        score = 0
      } else {
        score = scoreutil.strSimilarity(that.data.originalStr, res.result);
      }
      that.triggerEvent('recordSuccess', {
        tempFilePath: res.tempFilePath,
        score: score
      }, {
        bubbles: true,
        composed: true
      });
    };
 
    manager.onStart = function(res) {
      console.log("recordStart--onStart")
      that.triggerEvent('recordStart', res, {
        bubbles: true,
        composed: true
      });
    };

    manager.onError = function(res) {
      console.log("record onError"+res.retcode+res.msg)
      log.error("record onError:"+res.retcode+res.msg)
      isRecording = false;
      if(res.retcode == -30003){
        setTimeout(function () {
          wx.showToast({
            title: '录音时长太短,请放慢速度再读一遍~',
            icon: 'none',
            duration: 2000
          });
        }, 800)
      }

      that.triggerEvent('recordFail', res, {
        bubbles: true,
        composed: true
      });
    };
  },

  /**
   * 组件的方法列表
   */
  methods: {
    finish: function() {
      var that = this;
      that.setData({
        show: false
      });
      that.triggerEvent('modalFinish', {}, {
        bubbles: true,
        composed: true
      });
    },
    outerTap: function() {
      if (this.data.mask && this.data.mask.touchcalcel) {
        this.setData({
          show: false
        });
        this.triggerEvent('recordCancel', {}, {
          bubbles: true,
          composed: true
        });
      }
    }
  }
})